///////////////////////////////////////////////////////////////////////////////
//
// Licensed under the MIT License
//
// Copyright (c) 2010 Chris Colinsky
// All Rights Reserved
//
// version	0.5
// http://code.google.com/p/sugarcookie/
// http://code.google.com/p/sugarcookie/wiki/License
//
////////////////////////////////////////////////////////////////////////////////

package sugarcookie.effects {
	import flash.display.Sprite;
	import flash.display.BitmapData;
	import flash.display.Bitmap;
	import flash.geom.Matrix;
	import flash.display.GradientType;
	import flash.display.SpreadMethod;
	
	public class Reflection extends Sprite {
		private var _srcMC:Sprite;
		private var _alpha:Number;
		private var _distance:Number;
		private var _dropoff:Number;
		
		private var _bmd:BitmapData;
		private var _reflect_bmp:Bitmap;
		private var _gradient_mc:Sprite;
		
		public function Reflection(src:Sprite,alpha:Number=.4,distance:Number=0,dropoff:Number=0) {
			super();
			_srcMC = src;
			_alpha = alpha;
			_distance = distance;
			_dropoff = dropoff;
			createRefelection();
		}
		
		private function createRefelection():void {
			_bmd = new BitmapData(_srcMC.width,_srcMC.height,true,0xFFFFFF);
			_bmd.draw(_srcMC);
			
			_reflect_bmp = new Bitmap(_bmd);
			_reflect_bmp.scaleY = -1;
			_reflect_bmp.y = _srcMC.height*2 + _distance;
			_srcMC.addChild(_reflect_bmp);
			
			_gradient_mc = new Sprite();
			_srcMC.addChild(_gradient_mc);
			
			var fill:String = GradientType.LINEAR;
		 	var colors:Array = [0xFFFFFF, 0xFFFFFF];
		 	var alphas:Array = [_alpha, 0];
		  	var ratios:Array = [0, 50];
			var spread:String = SpreadMethod.PAD;
			
			var matrix:Matrix = new Matrix();
			var matrix_height:Number;
			if (_dropoff<=0) matrix_height = _srcMC.height;
			else matrix_height = _srcMC.height/_dropoff;
			matrix.createGradientBox(_srcMC.width, matrix_height, (90/180)*Math.PI, 0, 0);
			
			_gradient_mc.graphics.beginGradientFill(fill,colors,alphas,ratios,matrix,spread);  
		    _gradient_mc.graphics.drawRect(0,0,_srcMC.width,_srcMC.height);
		    _gradient_mc.y = _reflect_bmp.y - _reflect_bmp.height;
		    
		    _gradient_mc.cacheAsBitmap = true;
		    _reflect_bmp.cacheAsBitmap = true;
		    _reflect_bmp.mask = _gradient_mc;
		}
	}
}






